package cn.ulc.socket.weapp.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.util.CommonUtil;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.weapp.service.WxUserService;
import cn.ulc.store.dao.CompanyDao;
import cn.ulc.store.dao.CouponDao;
import cn.ulc.store.dao.PubDataDao;
import cn.ulc.store.dao.user.UserCouponDao;
import cn.ulc.store.dao.user.WeixinUserDao;
import cn.ulc.store.entity.Coupon;
import cn.ulc.store.entity.user.UserCoupon;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class WxUserServiceImpl extends BaseService implements WxUserService {

  private static final Log log = LogFactory.get();

  PubDataDao pubDataDao;
  CouponDao couponDao;
  UserCouponDao userCouponDao;
  WeixinUserDao userDao;

  @Override
  public WeixinUser findOne(Long id) {

    return userDao.findOne(id);
  }

  @Override
  public void executeBirthdayCheckJob() {
    String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_COUPON, "生日有礼");
    if(StrUtil.isBlank(code)) {
      log.info("生日有礼优惠券活动不存在");
      return;
    }
    Coupon couponTemplate = couponDao.findByType(getCurrentCompany().getId(), code);
    if(couponTemplate == null){
      log.info("生日有礼优惠券模板不存在");
      return;
    }
    List<WeixinUser> userList = userDao.findBirthdayList();
    if(userList != null && userList.size()>0){
      List<UserCoupon> couponList = new ArrayList<>();

      userList.stream().forEach(u -> {
        UserCoupon coupon = UserCoupon.builder()
            .wxUserId(u.getId())
            .memberId(u.getMemberId())
            .couponId(couponTemplate.getId())
            .code(createNo(couponList))
            .amount(couponTemplate.getAmount())
            .status(PubDataConstant.COUPON_STATUS_NOT_START)
            .isUsed(false)
            .isForever(couponTemplate.getIsForever())
            .beginTime(DateUtil.date())
            .endTime(couponTemplate.getEndDate())
            .isFull(couponTemplate.getIsFull())
            .fullAmount(couponTemplate.getFullAmount())
            .userScope(couponTemplate.getUserScope())
            .goodsId(couponTemplate.getGoodsId())
            .goodsTypeId(couponTemplate.getGoodsTypeId())
            .title(couponTemplate.getTitle().replace("{user}",u.getRealName()))
            .remark(couponTemplate.getRemark())
            .build();
        setBaseInfo(coupon);
        couponList.add(coupon);
      });
      userCouponDao.saveAll(couponList);
    }
  }

  private String createNo(List<UserCoupon> couponList){
    String code = CommonUtil.generateRandomChars(6);
    UserCoupon coupon = userCouponDao.findByCode(code);
    if(coupon != null){
      return createNo(couponList);
    }
    if(couponList.stream().filter(c->c.getCode().equals(code)).count()>0){
      return createNo(couponList);
    }
    return code;
  }
}
